using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
namespace Microsoft.Phone.Controls
{
	[TemplatePart(Name = "EmbeddedProgressBar", Type = typeof(ProgressBar)), TemplateVisualState(GroupName = "VisibilityStates", Name = "Normal"), TemplateVisualState(GroupName = "VisibilityStates", Name = "Hidden")]
	public class PerformanceProgressBar : Control
	{
		private const string EmbeddedProgressBarName = "EmbeddedProgressBar";
		private const string VisualStateGroupName = "VisibilityStates";
		private const string NormalState = "Normal";
		private const string HiddenState = "Hidden";
		private ProgressBar _progressBar;
		private static readonly PropertyPath ActualIsIndeterminatePath = new PropertyPath("ActualIsIndeterminate", new object[0]);
		private VisualStateGroup _visualStateGroup;
		private bool _cachedIsIndeterminate;
		private BindingExpression _cachedIsIndeterminateBindingExpression;
		public static readonly DependencyProperty ActualIsIndeterminateProperty = DependencyProperty.Register("ActualIsIndeterminate", typeof(bool), typeof(PerformanceProgressBar), new PropertyMetadata(false));
		public static readonly DependencyProperty IsIndeterminateProperty = DependencyProperty.Register("IsIndeterminate", typeof(bool), typeof(PerformanceProgressBar), new PropertyMetadata(false, new PropertyChangedCallback(PerformanceProgressBar.OnIsIndeterminatePropertyChanged)));
		public bool ActualIsIndeterminate
		{
			get
			{
				return (bool)base.GetValue(PerformanceProgressBar.ActualIsIndeterminateProperty);
			}
			set
			{
				base.SetValue(PerformanceProgressBar.ActualIsIndeterminateProperty, value);
			}
		}
		public bool IsIndeterminate
		{
			get
			{
				return (bool)base.GetValue(PerformanceProgressBar.IsIndeterminateProperty);
			}
			set
			{
				base.SetValue(PerformanceProgressBar.IsIndeterminateProperty, value);
			}
		}
		private static void OnIsIndeterminatePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
		{
			PerformanceProgressBar performanceProgressBar = d as PerformanceProgressBar;
			if (performanceProgressBar != null)
			{
				performanceProgressBar.OnIsIndeterminateChanged((bool)e.get_NewValue());
			}
		}
		public PerformanceProgressBar()
		{
			base.set_DefaultStyleKey(typeof(PerformanceProgressBar));
			base.add_Unloaded(new RoutedEventHandler(this.OnUnloaded));
			base.add_Loaded(new RoutedEventHandler(this.OnLoaded));
		}
		public override void OnApplyTemplate()
		{
			if (this._visualStateGroup != null)
			{
				this._visualStateGroup.remove_CurrentStateChanged(new EventHandler<VisualStateChangedEventArgs>(this.OnVisualStateChanged));
			}
			base.OnApplyTemplate();
			this._visualStateGroup = VisualStates.TryGetVisualStateGroup(this, "VisibilityStates");
			if (this._visualStateGroup != null)
			{
				this._visualStateGroup.add_CurrentStateChanged(new EventHandler<VisualStateChangedEventArgs>(this.OnVisualStateChanged));
			}
			this._progressBar = (base.GetTemplateChild("EmbeddedProgressBar") as ProgressBar);
			this._cachedIsIndeterminateBindingExpression = base.GetBindingExpression(PerformanceProgressBar.IsIndeterminateProperty);
			this.UpdateVisualStates(false);
		}
		private void OnVisualStateChanged(object sender, VisualStateChangedEventArgs e)
		{
			if (this.ActualIsIndeterminate && e != null && e.get_NewState() != null && e.get_NewState().get_Name() == "Hidden" && !this.IsIndeterminate)
			{
				this.ActualIsIndeterminate = false;
			}
		}
		private void OnIsIndeterminateChanged(bool newValue)
		{
			if (newValue)
			{
				this.ActualIsIndeterminate = true;
				this._cachedIsIndeterminate = true;
			}
			else
			{
				if (this.ActualIsIndeterminate && this._visualStateGroup == null)
				{
					this.ActualIsIndeterminate = false;
					this._cachedIsIndeterminate = false;
				}
			}
			this.UpdateVisualStates(true);
		}
		private void UpdateVisualStates(bool useTransitions)
		{
			VisualStateManager.GoToState(this, this.IsIndeterminate ? "Normal" : "Hidden", useTransitions);
		}
		private void OnUnloaded(object sender, RoutedEventArgs e)
		{
			if (this._progressBar != null)
			{
				this._cachedIsIndeterminate = this.IsIndeterminate;
				this._cachedIsIndeterminateBindingExpression = base.GetBindingExpression(PerformanceProgressBar.IsIndeterminateProperty);
				this._progressBar.set_IsIndeterminate(false);
			}
		}
		private void OnLoaded(object sender, RoutedEventArgs e)
		{
			if (this._progressBar != null)
			{
				if (this._cachedIsIndeterminateBindingExpression != null)
				{
					base.SetBinding(PerformanceProgressBar.IsIndeterminateProperty, this._cachedIsIndeterminateBindingExpression.get_ParentBinding());
				}
				else
				{
					this.IsIndeterminate = this._cachedIsIndeterminate;
				}
				FrameworkElement arg_59_0 = this._progressBar;
				DependencyProperty arg_59_1 = ProgressBar.IsIndeterminateProperty;
				Binding binding = new Binding();
				binding.set_Source(this);
				binding.set_Path(PerformanceProgressBar.ActualIsIndeterminatePath);
				arg_59_0.SetBinding(arg_59_1, binding);
			}
		}
		private static T FindFirstChildOfType<T>(DependencyObject root) where T : class
		{
			Queue<DependencyObject> queue = new Queue<DependencyObject>();
			queue.Enqueue(root);
			while (0 < queue.get_Count())
			{
				DependencyObject dependencyObject = queue.Dequeue();
				int num = VisualTreeHelper.GetChildrenCount(dependencyObject) - 1;
				while (0 <= num)
				{
					DependencyObject child = VisualTreeHelper.GetChild(dependencyObject, num);
					T t = child as T;
					if (t != null)
					{
						return t;
					}
					queue.Enqueue(child);
					num--;
				}
			}
			return default(T);
		}
	}
}
